package dtos

import (
	"strconv"

	"cashew.com/cashew_common/constants"
	"github.com/beego/beego/v2/core/validation"
)

type SendMessageParams struct {
	UserId       int64
	UserIdstr    string    `json:"user_id"`
	Gender       int       `json:"gender"`
	ToUserId     string    `json:"to_user_id"`
	MsgType      int       `json:"msg_type"`
	Msg          string    `json:"msg"`
	Duration     int       `json:"duration"`
	ChatScenario int       `json:"chat_scenario"`
	UserInfo     *UserInfo `json:"user_info,omitempty"`
	ToUserInfo   *UserInfo `json:"to_user_info,omitempty"`
}

type UserInfo struct {
	UserID    string `json:"user_id"`
	Gender    int    `json:"gender"`
	Nick      string `json:"nick"`
	HeadImg   string `json:"head_img"`
	Age       int    `json:"age"`
	FlagImage string `json:"flag_image"`
	Identity  int    `json:"identity"`
}

type SendMessageRes struct {
	ChatUnlock   bool  `json:"chat_unlock"`
	ChatPayPrice int64 `json:"chat_pay_price"`
	Identity     int   `json:"identity"`
	FreeCount    int   `json:"free_count"`
	SendStatus   bool  `json:"send_status"`
}

func (smp *SendMessageParams) Valid(v *validation.Validation) {
	// 验证UserId是否合法
	var err error
	smp.UserId, err = strconv.ParseInt(smp.UserIdstr, 10, 64)

	if err != nil {
		_ = v.SetError("UserId", "用户编号不合法")
		return
	}
	// 验证 ToUserId 是否合法
	toUserId, _ := strconv.ParseInt(smp.ToUserId, 10, 64)
	if smp.ToUserId == "" || toUserId <= 0 {
		_ = v.SetError("to_user_id", "消息接收者编号不合法")
		return
	}

	if smp.MsgType != constants.CHAT_MSG_TYPE_TEXT && smp.MsgType != constants.CHAT_MSG_TYPE_PIC && smp.MsgType != constants.CHAT_MSG_TYPE_AUDIO && smp.MsgType != constants.CHAT_MSG_TYPE_VIDEO && smp.MsgType != constants.CHAT_MSG_TYPE_GIFT {
		_ = v.SetError("msg_type", "消息类型不合法")
		return
	}

	if smp.Msg == "" {
		_ = v.SetError("msg", "消息内容不能为空")
		return
	}
	if smp.ChatScenario != constants.CHAT_SCENARIO_VIDEO_CALL && smp.ChatScenario != constants.CHAT_SCENARIO_PRIVATE {
		_ = v.SetError("chat_scenario", "聊天场景不合法")
		return
	}
	if smp.UserInfo == nil {
		_ = v.SetError("user_info", "用户信息不能为空")
		return
	}
	if smp.ToUserInfo == nil {
		_ = v.SetError("to_user_info", "接收用户信息不能为空")
		return
	}

	if smp.UserInfo.Identity != constants.IDENTITY_ANCHOR && smp.UserInfo.Identity != constants.IDENTITY_NORMAL_USER {
		_ = v.SetError("user_info.identity", "用户身份不合法")
		return
	}
	if smp.ToUserInfo.Identity != constants.IDENTITY_ANCHOR && smp.ToUserInfo.Identity != constants.IDENTITY_NORMAL_USER {
		_ = v.SetError("to_user_info.identity", "接收用户身份不合法")
		return
	}

	return
}
